package com.sigmatrix.h5.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.components.FeignManager;
import com.sigmatrix.h5.components.IsvTokenLoader;
import com.sigmatrix.h5.components.RedisClient;
import com.sigmatrix.h5.entity.DisDealerAcct;
import com.sigmatrix.h5.entity.Enterprise;
import com.sigmatrix.h5.entity.SmsConfig;
import com.sigmatrix.h5.enums.SmsTemplate;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.feign.args.SmsRequest;
import com.sigmatrix.h5.properties.SmsProperties;
import com.sigmatrix.h5.service.DisDealerAcctService;
import com.sigmatrix.h5.service.EnterpriseService;
import com.sigmatrix.h5.service.ShortMessageService;
import com.sigmatrix.h5.service.SmsConfigService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.SessionUtils;

/**
 * 
 *@ClassName: ShortMessageServiceImpl
 *@Description: 短信接口
 *@author MH
 *@date 2017年12月14日 下午1:34:06
 */
@Service("shortMessageServiceV1")
public class ShortMessageServiceImpl implements ShortMessageService{
	
	@Autowired
	private FeignManager feignManager;
	
	@Autowired
	private RedisClient redisClient;

	@Autowired
	private SmsProperties smsProperties;
	
	@Autowired
	private IsvTokenLoader isvTokenLoader;
	
	@Autowired
	protected SmsConfigService smsConfigService;
	
	@Autowired
	protected DisDealerAcctService disDealerAcctService;
	
	@Autowired
	protected EnterpriseService enterpriseService;
	
	@Override
	public void sendVerificationCode(String phoneNumber) {
		sendVerificationCode(phoneNumber, SessionUtils.getEseCode());
	}
	@Override
	public void sendVerificationCode(String phoneNumber,String eseCode) {
		SmsConfig template = smsConfigService.findByModuleAndEseCode(SmsTemplate.REBATE_VER_CODE.toString(), eseCode);
		Assert.notNull(template, "该企业没有配置短信模板");
		//生成一个6未的随机数
		String code = RandomStringUtils.random(6, "0123456789");
		//发送短信
		SmsRequest smsRequest = new SmsRequest();
		smsRequest.setPhoneNumber(phoneNumber);
		smsRequest.setTemplateCode(template.getTemplateCode());
		Map<String,String> map = new HashMap<String,String>();
		map.put("code", code);
		map.put("1", code);
		map.put("minute", smsProperties.getMinutie()+"");
		map.put("2", smsProperties.getMinutie()+"");
		smsRequest.setKeyValues(map);
//		feignManager.sendMessage(smsRequest,isvTokenLoader.getIsvToken(), eseCode);
		feignManager.sendMessageThroughApi(smsRequest,eseCode);
		//保存到redis,设置时效
		redisClient.set(IsvTokenLoader.SMS_CODE_PEFFIX+code, code, smsProperties.getMinutie(), TimeUnit.MINUTES);
	}


	@Override
	public void validateCode(String code) {
		SmsConfig template = smsConfigService.findByModuleAndEseCode(SmsTemplate.REBATE_VER_CODE.toString(), SessionUtils.getEseCode());
		if(CommonUtils.isEmpty(template)) return;
		Assert.notNull(code, "验证码不能为空");
		String smsCode = redisClient.get(IsvTokenLoader.SMS_CODE_PEFFIX+code);
		Assert.notNull(smsCode, "短信验证码不正确，或者已经超时");
		redisClient.delete(IsvTokenLoader.SMS_CODE_PEFFIX+code);
	}


	@Override
	public DisDealerAcct sendMessageByAcct(String acct) {
		Assert.notNull(acct, "账号不能为空");
		DisDealerAcct user = disDealerAcctService.findByUserName(acct);
		Assert.notNull(user, "账号错误,没有此用户");
		if(!CommonUtils.isCellPhoneNumber(user.getContact()))
			throw new MessageException("该账号没有绑定有效的手机号");
		Enterprise enterprise = enterpriseService.findById(user.getEnterpriseId());
		Assert.notNull(enterprise, "账号对应的企业ID不正确");
		sendVerificationCode(user.getContact(),enterprise.getCode());
		return user;
	}
}
